import { useState } from 'react';
import { Button, Space, Form, Row, Col, Table } from 'antd';
import { PAGINATION_CONFIG } from '@/constants';
import type { Cooperation } from './services';
import { CooperationIntence, CooperationWorkType, queryCooperationList } from './services'
import { useRequest } from 'ahooks';
import type { ColumnsType } from 'antd/es/table/interface';
import dayjs from 'dayjs';

const CooperationPage: React.FC<RoutePageProps> = () => {
  const [searchForm] = Form.useForm()
  const [searchParams, setSearchParams] = useState({
    pageNum: 1, pageSize: 10, 
  })
  const list = useRequest(() => queryCooperationList(searchParams), {
    refreshDeps: [searchParams],
  })

  const onFinish = () => {
    setSearchParams({
      ...searchParams,
      pageNum: 1,
    })
  }

  const columns: ColumnsType<Cooperation> = [
    {
      title: '申请人',
      dataIndex: 'username',
      width: 120,
    },
    {
      title: '联系电话',
      dataIndex: 'telephone',
      width: 140,
    },
    {
      title: '考察公司',
      dataIndex: 'intence',
      width: 140,
      render: val => CooperationIntence[val]
    },
    {
      title: '目标城市',
      dataIndex: 'cityName',
      width: 160,
    },
    {
      title: '合作方式',
      dataIndex: 'workType',
      width: 140,
      render: val => CooperationWorkType[val]
    },
    {
      title: '场地类型',
      dataIndex: 'itemType',
      width: 160,
      render: val => val || '--'
    },
    {
      title: '申请时间',
      dataIndex: 'createTime',
      width: 160,
      render: val => dayjs(val).format('YYYY-MM-DD')
    },
  ]
  
  return (
    <div className='container'>
      <div className="card search-card">
        <div className="form-area">
          <Form
            name='searchErrorForm' form={searchForm} 
            onFinish={onFinish}
          >
            <Row gutter={[20, 20]}>
              <Col span={6}>
                <Space>
                  <Button type='primary' htmlType='submit' loading={list.loading}>筛选</Button>
                </Space>
              </Col>
            </Row>
          </Form>
        </div>
      </div>
      <div className="card content-card">
        <Table<Cooperation>
          rowKey='stubId'
          loading={list.loading}
          dataSource={list.data?.data}
          columns={columns}
          scroll={{y: 700, x: 'max-content'}}
          pagination={{
            ...PAGINATION_CONFIG,
            current: searchParams.pageNum,
            pageSize: searchParams.pageSize,
            total: list.data?.totalCount || 0,
            onChange: (page, pageSize) => {
              setSearchParams({
                ...searchParams,
                pageNum: page === searchParams.pageNum ? 1 : page,
                pageSize,
              })
            }
          }}
        />
      </div>
    </div>
  );
};

export default CooperationPage;
